package sanshui.system.trade.service;


import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import sanshui.system.trade.common.CommonResponse;
import sanshui.system.trade.db.entity.TradeHisOpPos;
import sanshui.system.trade.factory.operate.OperationKey;
import sanshui.system.trade.factory.operate.OperationTo;
import sanshui.system.trade.factory.trade.strategy.StrategyCallType;
import sanshui.system.trade.factory.trade.strategy.StrategyResult;
import sanshui.system.trade.util.CommonTimeUtil;
import sanshui.system.trade.util.IdWorkerUtil;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class BuySellServiceImpl implements IBuySellService{

    @Resource
    RedisTemplate<String, String> redisTemplate;
    @Resource
    IdWorkerUtil idWorkerUtil;

    @Override
    public CommonResponse<Boolean> operation(String accId, BigDecimal price, BigDecimal num, String code, StrategyCallType operationType) {
        OperationTo operationTo = OperationTo.builder()
                .code(code)
                .num(num.toPlainString())
                .price(price.toPlainString())
                .opType(operationType.name()).build();
        log.info("发起操作, operationTo:{}", operationTo);
        redisTemplate.opsForValue().set(OperationKey.TRADE_OPS.name(), JSONObject.toJSONString(operationTo), 1000, TimeUnit.MICROSECONDS);
        return CommonResponse.ok(true);
    }

    @Override
    public CommonResponse<Boolean> operationAsync(String accId, BigDecimal price, BigDecimal num, String code, StrategyResult endStrategyCall) {
        OperationTo operationTo = OperationTo.builder()
                .code(code)
                .num(num.toPlainString())
                .price(price.toPlainString())
                .opType(endStrategyCall.getStrategyCallType().name()).build();
        log.info("发起异步操作, operationTo:{}", operationTo);
//        redisTemplate.opsForValue().set(OperationKey.TRADE_OPS.name(), JSONObject.toJSONString(operationTo), 1000, TimeUnit.MICROSECONDS);
        return CommonResponse.ok(true);
    }

    @Override
    public boolean sell(StrategyResult endStrategyCall) {
        String tradeTime = endStrategyCall.getTradeTime();
        if (!CommonTimeUtil.compare(CommonTimeUtil.getTimestamp(tradeTime), LocalDateTime.now(), 20, ChronoUnit.SECONDS)){
            // 在5秒内, 可以交易
            log.info("发生交易，endStrategyCall:{}", endStrategyCall);
            //redisTemplate.opsForValue().set(OperationKey.TRADE_OPS.name(), JSONObject.toJSONString(operationTo), 1000, TimeUnit.MICROSECONDS);
        }
        return false;
    }

    @Override
    public boolean buy(StrategyResult endStrategyCall) {
        String tradeTime = endStrategyCall.getTradeTime();
        if (!CommonTimeUtil.compare(CommonTimeUtil.getTimestamp(tradeTime), LocalDateTime.now(), 20, ChronoUnit.SECONDS)){
            // 在5秒内, 可以交易
            log.info("发生交易，endStrategyCall:{}", endStrategyCall);
            //redisTemplate.opsForValue().set(OperationKey.TRADE_OPS.name(), JSONObject.toJSONString(operationTo), 1000, TimeUnit.MICROSECONDS);
        }
        return false;
    }
}
